package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

import models.Oficina;
import models.Sistema;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ComboPanel extends javax.swing.JFrame {
	
	private DefaultComboBoxModel comboModel= new DefaultComboBoxModel();
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JLabel lblSeleccion;
	private JComboBox combo;
	private PantallaPrincipal parent;
	private JFrame window= this;
	private List<Oficina> oficinas;
	private ActionsListener listener=new ActionsListener();
	private Sistema sis = Sistema.getInstancia();
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public ComboPanel(PantallaPrincipal parent, List<Oficina> list) {
		super();
		this.parent=parent;
		this.oficinas = list;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				for(Oficina oficina : oficinas){
					comboModel.addElement(oficina.getNombre());
				}
				combo = new JComboBox();
				getContentPane().add(combo);
				combo.setModel(comboModel);
				combo.setPreferredSize(new java.awt.Dimension(237, 21));
				combo.setBounds(58, 34, 237, 21);
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setPreferredSize(new java.awt.Dimension(94, 21));
				btnAceptar.setBounds(40, 67, 94, 21);
				btnAceptar.addActionListener(listener);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setPreferredSize(new java.awt.Dimension(102, 21));
				btnCancelar.setBounds(223, 67, 102, 21);
				btnCancelar.addActionListener(listener);
			}
			{
				lblSeleccion = new JLabel();
				getContentPane().add(lblSeleccion);
				lblSeleccion.setText("Seleccione la oficina que desea darle de baja");
				lblSeleccion.setPreferredSize(new java.awt.Dimension(305, 16));
				lblSeleccion.setBounds(40, 7, 305, 16);
			}
			pack();
			this.setSize(373, 160);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class ActionsListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent event) {
			Object source= event.getSource();
			if(source == btnCancelar){
				window.dispose();
				parent.setVisible(true);
			}
			else if(source == btnAceptar)
			{
				
				String oficina = (String)combo.getSelectedItem();
				if (oficina != null){
					sis.bajaOficinaDeCorreo(oficina);
					comboModel.removeElement(oficina);
					JOptionPane.showMessageDialog(null, "Se ha eliminado la oficina: " + oficina);
					for(Oficina Oficina : oficinas){
						System.out.println("Quedaron las oficinas siguientes: ");
						System.out.println(Oficina.getNombre());
					}
				}
				else {
					window.dispose();
					parent.setVisible(true);
					JOptionPane.showMessageDialog(null, "No hay mas oficinas para eliminar ");
				}
			}
		}
	}
	
	public DefaultComboBoxModel getComboModel() {
		return comboModel;
	}
}
